<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * This controler handles the login, logout, register and delete user
 * @author ControllerTeam
 *
 */
class login extends CI_Controller {

	public function __construct()
	{
		parent::__construct();
		require_once('user.php');
		$this->user=new User();
	}

	/**
	 * checks if the current user is alread logged in, if not it will try to log him in.<br>
	 * If no valid login data are provided, the user will be redirected to the main page
	 */
	function loginUser()
	{
		if($this->user->logged_in()){
			$data['redirect_message'] = "You are already logged in";
			$this->load->view('redirect', $data);
			return;
		}

		if(empty($_POST['username']))
		{
			$data['redirect_message'] = "No username entered";
			$this->load->view('redirect', $data);
			return;
		}
		if(empty($_POST['password']))
		{
			$data['redirect_message'] = "No password entered";
			$this->load->view('redirect', $data);
			return;
		}
		$username = trim($_POST['username']);
		$password = trim($_POST['password']);
		if( $this->user->login($username, $password))
		{
			$data['redirect_message'] = "You are now logged in";
			$this->load->view('redirect', $data);
			return;
		} else {
			$data['redirect_message'] = "Invalid login data";
			$this->load->view('redirect', $data);
			return;
		}
	}

	/**
	 * logout the current user
	 */
	function logout(){
		if($this->user->logged_in()){
			$this->user->logout();
			$data['redirect_message'] = "Successfully logged out";
			$this->load->view('redirect', $data);
		} else {
			$data['redirect_message'] = "You were not logged in";
			$this->load->view('redirect', $data);
		}
	}

	/**
	 * displays the view on which the user can register himself (if he is not already logged in
	 */
	function register(){
		$this->load->view('header');


		$loggedIn = $this->user->logged_in();

		if($loggedIn){
			$data['redirect_message'] = "You are already logged in";
			$this->load->view('redirect', $data);
			return;
		}

		$loginData['isLoggedIn'] = $loggedIn;
		$this->load->view('login', $loginData);

		$menu['activeController'] = 'index';
		$this->load->view('menu',$menu);

		$this->load->view('register');
		$this->load->view('footer');
	}

	/**
	 * register a new user in the database. after this, the user can login.
	 */
	function registerPost(){
		if($this->user->logged_in()){
			echo "already logged in";
			return false;
		}

		if(empty($_POST['username']))
		{
			echo "no username";
			return false;
		}
		
		if ( $this->user->checkIfUsernameExist($_POST['username'])){
			echo "username already exists";
			return false;
		}
		if(empty($_POST['password']))
		{
			echo "no password";
			return false;
		}
		if(empty($_POST['eMail']))
		{
			echo "no mail";
			return false;
		}
		if(strnatcmp ( $_POST['passwordRpt'] , $_POST['password'])!=0){
			echo "repeated password does not match";
			return false;
		}
		
		//get all information and store it in the database

		$username = trim($_POST['username']);
		$password = trim($_POST['password']);
		$mail = trim($_POST['eMail']);

		$userdata->Name = $_POST['lastName'];
		$userdata->Surname = $_POST['name'];
		$userdata->Pro = $_POST['type'];
		$userdata->Size = $_POST['size'];
		$userdata->Weight = $_POST['weight'];
		$userdata->Gender = $_POST['gender'];
		$userdata->City = $_POST['location'];
		$userdata->Country = $_POST['country'];
		$userdata->Birthday = $_POST['birthday'];

		$error = $this->user->register( $username, $password, $mail,1,$userdata);
		
		if($error != 1){
			echo $error;
		}
		$this->load->view('redirect');
		
	}

	/**
	 * delete the current user from the database, if he is logged in.
	 */
	function deleteAccount(){
		if(!$this->user->logged_in()){
			$data['redirect_message'] = "You are not logged in";
			$this->load->view('redirect', $data);
			return;
		} else {
			$this->user->remove();
			$this->user->logout();
			$this->load->view('redirect');
		}
	}

}